<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.artNo" placeholder="货号" clearable style="width: 130px;" class="filter-item" />
      <el-select v-model="listQuery.status" placeholder="处理状态" clearable filterable style="width: 140px; margin-left: 10px;" class="filter-item">
        <el-option v-for="item in statusOptions" :key="item.key" :label="item.display_name" :value="item.key" />
      </el-select>
      <el-select v-model="listQuery.needNotice" placeholder="是否到货提醒" clearable filterable style="width: 140px; margin-left: 10px;" class="filter-item">
        <el-option v-for="item in needNoticeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
      </el-select>
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">
        查询
      </el-button>
      <el-button v-waves class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-refresh" @click="handleRefresh">
        重置
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="info" icon="el-icon-circle-plus-outline" @click="handleAdd">
        添加
      </el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" :header-cell-style="headerCellStyle" :cell-style="{padding:'0px'}" border stripe element-loading-text="拼命加载中">
      <el-table-column label="货号" align="center" width="160px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.artNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="80px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.status | statusFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="尺码" align="center" width="60px">
        <template slot-scope="{row}">
          <span style="float: right;">{{ row.size }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" width="60px">
        <template slot-scope="{row}">
          <span style="float: right;">{{ row.qty }}</span>
        </template>
      </el-table-column>
      <el-table-column label="可接受最大折扣" align="center" width="120px">
        <template slot-scope="{row}">
          <span style="float: right;">{{ row.maxDiscount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="可接受调货日期" align="center" width="120px">
        <template slot-scope="{row}">
          <span>{{ row.expectDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否到货提醒" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.needNotice | needNoticeFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" min-width="110px">
        <template slot-scope="{row}">
          <span style="float: left;">{{ row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="110px" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog title="新增找货需求" :visible.sync="addVisible">
      <el-form ref="addForm" :rules="addRules" :model="add" label-width="120px" style="margin-left:0px;">
        <el-form-item label="货号" prop="artNo">
          <el-input v-model="add.artNo" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="尺码" prop="size">
          <el-input v-model="add.size" style="width: 400px;" />
        </el-form-item>
        <el-form-item label="数量" prop="qty">
          <el-input v-model="add.qty" type="number" step="1" :min="1" :max="999" style="width: 100px;" />
        </el-form-item>
        <el-form-item label="可接受最大折扣" prop="maxDiscount">
          <el-input v-model="add.maxDiscount" type="number" step="1" :min="0.01" :max="100" style="width: 100px;" />
        </el-form-item>
        <el-form-item label="可接受调货日期" prop="expectDate">
          <el-date-picker
            v-model="add.expectDate"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder=""
          />
        </el-form-item>
        <el-form-item label="到货提醒" prop="needNotice">
          <el-select v-model="add.needNotice" placeholder="是否到货提醒" clearable filterable style="width: 140px;" class="filter-item">
            <el-option v-for="item in needNoticeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="add.remark" type="textarea" :rows="4" style="width: 400px;" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取消</el-button>
        <el-button type="primary" @click="addConfirm()">确认</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { fetchEnum } from '@/api/system-api'
import { getGoodsDemandByPage, addGoodsDemand } from '@/api/distributor/distribution-api'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { toOptionsList, toKeyValueMap } from '@/utils/index'

var statusKeyValue = {}
var needNoticeKeyValue = {}

export default {
  name: 'DistributorGoodsDemand',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      return statusKeyValue[status]
    },
    needNoticeFilter(needNotice) {
      return needNoticeKeyValue[needNotice]
    }
  },
  data() {
    return {
      statusOptions: [],
      needNoticeOptions: [],
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        artNo: null,
        status: null,
        needNotice: null
      },

      listLoading: true,
      list: null,
      total: 0,

      addVisible: false,
      add: {
        artNo: null,
        size: null,
        qty: null,
        maxDiscount: null,
        expectDate: null,
        needNotice: null,
        remark: null
      },
      addRules: {
        artNo: [{
          required: true, message: '请输入货号', trigger: 'blur'
        }],
        size: [{
          required: true, message: '请输入尺码', trigger: 'blur'
        }],
        qty: [{
          required: true, message: '请输入数量', trigger: 'blur'
        }],
        maxDiscount: [{
          required: true, message: '请输入可接受最大折扣', trigger: 'blur'
        }],
        expectDate: [{
          required: true, message: '请输入可接受调货日期', trigger: 'blur'
        }],
        needNotice: [{
          required: true, message: '请输入到货提醒', trigger: 'blur'
        }]
      }

    }
  },
  async created() {
    await this.initLoad()
    await this.getList()
  },
  methods: {
    async initLoad() {
      const dataDictsParam = ['GOODS_DEMAND_STATUS', 'GOODS_DEMAND_NEED_NOTICE']
      const { data } = await fetchEnum(dataDictsParam)
      for (const dataDict of data) {
        if (dataDict.code === 'GOODS_DEMAND_STATUS') {
          if (dataDict.options) {
            this.statusOptions = toOptionsList(dataDict.options, 'code', 'name')
            statusKeyValue = toKeyValueMap(this.statusOptions, 'key', 'display_name')
          }
        }
        if (dataDict.code === 'GOODS_DEMAND_NEED_NOTICE') {
          if (dataDict.options) {
            this.needNoticeOptions = toOptionsList(dataDict.options, 'code', 'name')
            needNoticeKeyValue = toKeyValueMap(this.needNoticeOptions, 'key', 'display_name')
          }
        }
      }
      this.initListQuery()
    },
    getList() {
      this.listLoading = true
      getGoodsDemandByPage(this.listQuery).then(response => {
        const { data } = response
        this.list = data.result
        this.total = data.totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 1 * 1000)
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
        this.listLoading = false
      })
    },
    initListQuery() {
      this.listQuery = {
        pageNo: 1,
        pageSize: 20,
        artNo: null,
        status: null,
        needNotice: null
      }
    },

    handleFilter() {
      this.getList()
    },
    handleRefresh() {
      this.initListQuery()
      this.getList()
    },
    handleAdd() {
      this.add = {
        artNo: null,
        size: null,
        qty: 10,
        maxDiscount: 1.00,
        expectDate: null,
        needNotice: this.needNoticeOptions[0].key,
        remark: null
      }
      this.addVisible = true
    },
    headerCellStyle({ row, rowIndex }) {
      return 'background-color: #42b983; color: #FFF;'
    },

    addConfirm() {
      this.$refs.addForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.add))
        addGoodsDemand(data).then(response => {
          this.$notify({
            title: '成功',
            message: '添加成功',
            type: 'success',
            duration: 2000
          })
          this.addVisible = false
          this.getList()
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    }

  }
}

</script>
